﻿using System;
using System.Collections.Generic;

namespace Moonwish.Core
{
    /// <summary>
    /// 查询基类by zhlin 2011-06-13。
    /// </summary>
    public abstract class QueryBase<T>
    {
        protected readonly Request Request;
        protected readonly Dictionary<string, string> Map = new Dictionary<string, string>();

        protected QueryBase(Request request)
        {
            Request = request;
        }
        /// <summary>
        /// 根据FieldName映射到数据库中的对应字段
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        protected string ToTableColumn(string column)
        {
            if (string.IsNullOrEmpty(column))
                throw new InvalidOperationException("查询条件的字段为空或者长度为零。");

            if (!Map.ContainsKey(column.ToLower()))
                throw new InvalidOperationException(string.Format("系统不支持按“{0}”条件进行查询。", column));

            return Map[column.ToLower()];
        }

        /// <summary>
        /// 执行查询。
        /// </summary>
        /// <returns></returns>
        public abstract List<T> Execute();

        /// <summary>
        /// 构建SQL。
        /// </summary>
        /// <returns></returns>
        protected abstract string BuildSql();
    }
}